/*
 *
 * 通用方法
 */
export default {
  // 序列化数制 &id=2&vaule=3方式显示
  serialize(form) {
    var parts = [];
    for (var pair in form) {
      parts.push(pair + '=' + form[pair]);
    }
    return parts.join('&');
  },
  // 将对名转成FormData，可转数组 this.objectToFormData(obj)
  objectToFormData(obj, form, namespace) {
    var _formData = form || new FormData();
    var formKey;
    if (obj instanceof Array) {
      for (var a = 0; a < obj.length; a++) {
        if (typeof obj[a] === 'object' && !(obj[a] instanceof File)) {
          this.objectToFormData(obj[a], _formData, namespace + '[' + a + ']');
        } else {
          // 若是数组则在关键字后面加上[]
          _formData.append(namespace + '[' + a + ']', obj[a]);
        }
      }
    } else {
      for (var property in obj) {
        if (obj.hasOwnProperty(property) && obj[property]) {
          if (namespace) {
            // 若是对象，则这样
            formKey = namespace + '.' + property;
          } else {
            formKey = property;
          }
          if (typeof obj[property] === 'object' && !(obj[property] instanceof File)) {
            this.objectToFormData(obj[property], _formData, formKey);
          } else {
            _formData.append(formKey, obj[property]);
          }
        }
      }
    }
    return _formData;
  }
};
